<?php

namespace app\install\core;

class InstallCore
{
    public static function testdbHand()
    {
        $dbport = request()->param("dbport", "");
        $dbhost = request()->param("dbhost", "");
        $dbuser =request()->param("dbuser", "");
        $dbpwd = request()->param("dbpwd", "");
        $dbname = request()->param("dbname", "");
        if ($dbport != "" && $dbhost != "") {
            $dbhost = $dbport != '3306' ? $dbhost . ':' . $dbport : $dbhost;
        }

        if ($dbhost == '' || $dbuser == '') {
            return [
                'code' => 0,
                'msg' => '数据库账号或密码不能为空'
            ];
        }

        if (!function_exists("mysqli_connect")) {
            return [
                'code' => 0,
                'msg' => 'mysqli扩展类必须开启'
            ];
        }

        $conn = @mysqli_connect($dbhost, $dbuser, $dbpwd);
        if ($conn) {
            if (empty($dbname)) {
                return [
                    'code' => 1,
                    'msg' => '数据库连接成功',
                    'data' => [
                        'status' => 1
                    ]
                ];
            } else {
                try {
                    $has = @mysqli_select_db($conn, $dbname);
                }catch (\Exception $e){
                    return [
                        'code' => 1,
                        'msg' => '数据库不存在,自动创建',
                        'data' => [
                            'status' => 1
                        ]
                    ];
                }

                if (!$has) {
                    return [
                        'code' => 1,
                        'msg' => '数据库不存在,自动创建',
                        'data' => [
                            'status' => 1
                        ]
                    ];
                }else{
                    return [
                        'code' => 1,
                        'msg' => '数据库存在，覆盖数据库',
                        'data' => [
                            'status' => 2
                        ]
                    ];
                }
            }
            @mysqli_close($conn);
        } else {
            return [
                'code' => 0,
                'msg' => '数据库连接失败！'
            ];
        }
        return [
            'code' => 1,
            'msg' => '数据库链接成功',
            'data' => [
                'status' => 1
            ]
        ];

    }
}